﻿using MaterialDesignThemes.Wpf;
using SvgViewer.Entities;
using System;
using System.Text;
using SvgViewer.Core;
using SvgViewer.Utilities;

namespace SvgViewer.ViewModel;

/// <summary>
/// 导出图像窗口
/// </summary>
public class ExportViewModel : ViewModelBase
{
    private bool          codeMode;
    private StringBuilder content;

    /// <summary>
    /// 构造
    /// </summary>
    /// <param name="info">Svg信息</param>
    public ExportViewModel(SvgInfo info)
    {
        content = new StringBuilder();
        content.AppendLine();
        var lines = info.Content.Split(new[] {
            "\r\n"
        }, StringSplitOptions.RemoveEmptyEntries);

        foreach (var line in lines)
        {
            content.AppendLine($"                {line}");
        }

        Property = new ExportProperty(new PaletteHelper().GetTheme().PrimaryMid.Color, info.ImageSource);
    }

    /// <summary>
    /// 
    /// </summary>
    protected override void OnInitializeInRuntime()
    {
        base.OnInitializeInRuntime();
        SaveAsCommand   = new RelayCommand(_ => SaveAs());
        ResetCommand    = new RelayCommand(_ => Reset());
        CodeModeCommand = new RelayCommand(_ => BuildXaml());

        PropertyModeCommand = new RelayCommand(_ => {
            CodeMode = false;
        });
    }


#region Command...

    /// <summary>
    /// 另存在命令
    /// </summary>
    public RelayCommand SaveAsCommand { get; private set; }

    /// <summary>
    /// 重置命令
    /// </summary>
    public RelayCommand ResetCommand { get; private set; }

    /// <summary>
    /// 切换代码模式命令
    /// </summary>
    public RelayCommand CodeModeCommand { get; private set; }

    /// <summary>
    /// 切换属性模式命令
    /// </summary>
    public RelayCommand PropertyModeCommand { get; private set; }

#endregion

    /// <summary>
    /// 是否代码模式
    /// </summary>
    public bool CodeMode {
        get => codeMode;
        private set => SetValue(ref codeMode, value, nameof(CodeMode));
    }

    /// <summary>
    /// 获取图像导出属性
    /// </summary>
    public ExportProperty Property { get; }

#region Methods...

    private void SaveAs()
    {
        ViewService.Show("export");
    }

    private void Reset()
    {
        Property.Reset(new PaletteHelper().GetTheme().PrimaryMid.Color);
    }


    private void BuildXaml()
    {
        CodeMode = true;
        var xaml = Properties.Resources.SvgTemplate.Replace("{Size}", $"{Property.Size}").
            Replace("{BgRotate}",       $"{Property.BackgroundRotate}").Replace("{Padding}", $"{Property.PaddingSize}").
            Replace("{Margin}",         $"{Property.Margin}").Replace("{ImageContent}", $"{content}").
            Replace("{Rotate}",         $"{Property.Rotate}").Replace("{ScaleX}", $"{Property.ScaleXValue}").
            Replace("{ScaleY}",         $"{Property.ScaleYValue}").
            Replace("{Background}",     LanguageManager.Instance.GetText("ExportView.Background")).
            Replace("{Image content}",  LanguageManager.Instance.GetText("ExportView.ImageContent")).
            Replace("{Image rotation}", LanguageManager.Instance.GetText("ExportView.ImageRotation"));
        if (Property.SelectedBackgroundIndex == 0)
        {
            xaml = xaml.Replace("{BgColor}", "{x:Null}").Replace("{PathData}", "{x:Null}");
        }
        else
        {
            xaml = xaml.Replace("{BgColor}", $"{Property.BackgroundColor}").
                Replace("{PathData}", $"{Property.SelectedBackground}");
        }

        Property.Xaml = xaml;
    }

#endregion
}